package src.core;

import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AudioManager {
    private static AudioManager instance;
    private Clip backgroundMusic;
    private final Map<String, Clip> soundEffects;
    private float volume = 1.0f;
    private float shootVolume = 1.0f;  // 射击音效的音量
    private boolean isMuted = false;

    private AudioManager() {
        soundEffects = new HashMap<>();
        loadSoundEffects();
    }

    public static AudioManager getInstance() {
        if (instance == null) {
            instance = new AudioManager();
        }
        return instance;
    }

    private void loadSoundEffects() {
        // 加载音效
        loadSoundEffect("shoot", "src/resource/audio/shoot09.wav");
        // loadSoundEffect("zombie_hit", "src/resource/audio/zombie_hit.wav");
        // loadSoundEffect("zombie_die", "src/resource/audio/zombie_die.wav");
        // loadSoundEffect("player_hit", "src/resource/audio/player_hit.wav");
    }

    private void loadSoundEffect(String name, String path) {
        try {
            URL url = getClass().getClassLoader().getResource(path);
            if (url == null) {
                System.err.println("无法加载音效: " + path);
                return;
            }
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            soundEffects.put(name, clip);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            System.err.println("加载音效失败: " + name + " - " + e.getMessage());
        }
    }

    public void playBackgroundMusic(String path) {
        try {
            if (backgroundMusic != null) {
                backgroundMusic.stop();
                backgroundMusic.close();
            }

            URL url = getClass().getClassLoader().getResource(path);
            if (url == null) {
                System.err.println("无法加载背景音乐: " + path);
                return;
            }

            AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
            backgroundMusic = AudioSystem.getClip();
            backgroundMusic.open(audioIn);
            
            // 设置循环播放
            backgroundMusic.loop(Clip.LOOP_CONTINUOUSLY);
            
            // 设置音量
            setVolume(volume);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            System.err.println("播放背景音乐失败: " + e.getMessage());
        }
    }

    public void playSoundEffect(String name) {
        if (isMuted) return;
        
        Clip clip = soundEffects.get(name);
        if (clip != null) {
            if (name.equals("shoot")) {
                // 设置射击音效的音量
                FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                float dB = (float) (Math.log(shootVolume) / Math.log(10.0) * 20.0);
                gainControl.setValue(dB);
            }
            clip.setFramePosition(0);
            clip.start();
        }
    }

    // 设置射击音效的音量
    public void setShootVolume(float volume) {
        this.shootVolume = Math.max(0.0f, Math.min(1.0f, volume));
    }

    // 获取射击音效的音量
    public float getShootVolume() {
        return shootVolume;
    }

    public void setVolume(float volume) {
        this.volume = Math.max(0.0f, Math.min(1.0f, volume));
        
        if (backgroundMusic != null) {
            FloatControl gainControl = (FloatControl) backgroundMusic.getControl(FloatControl.Type.MASTER_GAIN);
            float dB = (float) (Math.log(this.volume) / Math.log(10.0) * 20.0);
            gainControl.setValue(dB);
        }
    }

    public void toggleMute() {
        isMuted = !isMuted;
        if (backgroundMusic != null) {
            if (isMuted) {
                backgroundMusic.stop();
            } else {
                backgroundMusic.start();
            }
        }
    }

    public void stopBackgroundMusic() {
        if (backgroundMusic != null) {
            backgroundMusic.stop();
        }
    }

    public void cleanup() {
        stopBackgroundMusic();
        if (backgroundMusic != null) {
            backgroundMusic.close();
        }
        for (Clip clip : soundEffects.values()) {
            clip.close();
        }
        soundEffects.clear();
    }
} 